package com.mpyy.handler.inventory;

import com.alibaba.fastjson.JSON;
import com.mpyy.pojo.*;
import com.mpyy.service.inventory.ProductBreakageServiceInter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;

/**
 * @version 1.0
 * @Description ProductBreakageController
 * @Author JW
 * @Date 2022-03-07 22:19
 */
@RestController
public class ProductBreakageController {
    @Autowired
    @Qualifier(value = "productBreakageServiceImpl")
    private ProductBreakageServiceInter productBreakageServiceInter;

    //提交报损商品信息
    @RequestMapping(value = {"/updateBreakageDescProcess/{proBeakMoreDesc}"}, method = RequestMethod.POST)
    public String updateBreakageDescProcess(@PathVariable String proBeakMoreDesc,ProductBreakage productBreakage) throws Exception {
        //创建一个StringBuffer对象，获取页面传递过来的报损商品详细数据，拼接还原后加入StringBuffer中
        StringBuffer stringBuffer = new StringBuffer();
        stringBuffer.append("[" + proBeakMoreDesc + "]");

        //调用自定义的转换工具类
        List<ProductBreakageDesc> productBreakageDescObjs = InventoryUtil.decomposeJsonArray(stringBuffer, ProductBreakageDesc.class);

        //判断新增是否成功
        boolean addIsSuccess = productBreakageServiceInter.insertProBreakageIsSuccess(productBreakage, productBreakageDescObjs);
        return JSON.toJSONString(addIsSuccess);
    }

    //提交报溢商品信息
    @RequestMapping(value = {"/updateMoreDescProcess/{proBeakMoreDesc}"}, method = RequestMethod.POST)
    public String updateMoreDescProcess(@PathVariable String proBeakMoreDesc,ProductBreakage productBreakage) throws Exception {
        //创建一个报溢单据对象
        ProductOverflow productOverflow = new ProductOverflow();

        //通过set方法为报溢单据对象赋值
        productOverflow.setProMoreId(productBreakage.getProBadId());
        productOverflow.setDepotId(productBreakage.getDepotId());
        productOverflow.setProMoreDate(productBreakage.getProBadDate());
        productOverflow.setEmpId(productBreakage.getEmpId());
        productOverflow.setProMoreComment(productBreakage.getProBadComment());

        //创建一个StringBuffer对象，获取页面传递过来的报损商品详细数据，拼接还原后加入StringBuffer中
        StringBuffer stringBuffer = new StringBuffer();
        stringBuffer.append("[" + proBeakMoreDesc + "]");

        //调用自定义的转换工具类
        List<ProductOverflowDesc> productOverflowDescObjs = InventoryUtil.decomposeJsonArray(stringBuffer, ProductOverflowDesc.class);

        boolean addIsSuccess = productBreakageServiceInter.insertProMoreIsSuccess(productOverflow, productOverflowDescObjs);
        return JSON.toJSONString(addIsSuccess);
    }

    //动态获取仓库下拉框信息
    @RequestMapping(value = {"/searchDepotInfoProcess"},method = RequestMethod.GET)
    public String searchDepotInfoProcess(){
        List<DepotInfo> depotInfo = productBreakageServiceInter.searchDepotInfo();
        return JSON.toJSONString(depotInfo);
    }

    //动态获取经办人下拉框信息
    @RequestMapping(value = {"/searchHandlePersonInfoProcess"},method = RequestMethod.GET)
    public String searchHandlePersonInfoProcess(){
        List<EmployeeInfo> handlePersonInfo = productBreakageServiceInter.searchHandlePersonInfo();
        return JSON.toJSONString(handlePersonInfo);
    }

    //展示报损单据的信息
    @RequestMapping(value = {"/searchProductBreakageInfoProcess"},method = RequestMethod.GET)
    public String searchProductBreakageInfoProcess(ProductBreakage productBreakage, Paging paging){
        productBreakage.setPaging(paging);

        List<ProductBreakage> productBreakages = productBreakageServiceInter.searchProductBreakageInfo(productBreakage);

        productBreakage.setPaging(null);
        int size = productBreakageServiceInter.searchProductBreakageInfo(productBreakage).size();
        HashMap<Object, Object> productBreakagesHashMap = new HashMap<>();
        productBreakagesHashMap.put("code",0);
        productBreakagesHashMap.put("msg","");
        productBreakagesHashMap.put("count",size);
        productBreakagesHashMap.put("data",productBreakages);
        return JSON.toJSONString(productBreakagesHashMap);
    }

    //根据商品报损编号获取报损详情信息
    @RequestMapping(value = {"/searchBreakageDescInfoProcess/{proBadId}"},method = RequestMethod.GET)
    public String searchBreakageDescInfoProcess(@PathVariable(name = "proBadId") String proBadId,
                                                ProductBreakageDesc productBreakageDesc, Paging paging){
        productBreakageDesc.setPaging(paging);
        productBreakageDesc.setProBadId(proBadId);
        List<ProductBreakageDesc> productBreakageDescs = productBreakageServiceInter.searchBreakageDescInfo(productBreakageDesc);

        HashMap<Object, Object> productBreakageDescsHashMap = new HashMap<>();
        productBreakageDescsHashMap.put("code",0);
        productBreakageDescsHashMap.put("msg","");
        productBreakageDescsHashMap.put("count",productBreakageDescs.size());
        productBreakageDescsHashMap.put("data",productBreakageDescs);
        return JSON.toJSONString(productBreakageDescsHashMap);
    }

    //展示报溢单据的信息
    @RequestMapping(value = {"/searchProductOverflowInfoProcess"},method = RequestMethod.GET)
    public String searchProductOverflowInfoProcess(ProductBreakage productBreakage, Paging paging){
        productBreakage.setPaging(paging);

        List<ProductBreakage> productOverflow = productBreakageServiceInter.searchProductOverflowInfo(productBreakage);

        productBreakage.setPaging(null);
        int size = productBreakageServiceInter.searchProductOverflowInfo(productBreakage).size();

        HashMap<Object, Object> productOverflowHashMap = new HashMap<>();
        productOverflowHashMap.put("code",0);
        productOverflowHashMap.put("msg","");
        productOverflowHashMap.put("count",size);
        productOverflowHashMap.put("data",productOverflow);
        return JSON.toJSONString(productOverflowHashMap);
    }

    //根据商品报溢编号获取报溢详情信息
    @RequestMapping(value = {"/searchOverflowDescInfoProcess/{proBadId}"},method = RequestMethod.GET)
    public String searchOverflowDescInfoProcess(@PathVariable(name = "proBadId") String proBadId,
                                                ProductBreakageDesc productBreakageDesc, Paging paging){
        productBreakageDesc.setPaging(paging);
        productBreakageDesc.setProBadId(proBadId);
        List<ProductBreakageDesc> productOverflowDescs = productBreakageServiceInter.searchOverflowDescInfo(productBreakageDesc);

        HashMap<Object, Object> productOverflowDescsHashMap = new HashMap<>();
        productOverflowDescsHashMap.put("code",0);
        productOverflowDescsHashMap.put("msg","");
        productOverflowDescsHashMap.put("count",productOverflowDescs.size());
        productOverflowDescsHashMap.put("data",productOverflowDescs);
        return JSON.toJSONString(productOverflowDescsHashMap);
    }
    //树形控件
    @RequestMapping(value = {"/searchTreeControlProcess"},method = RequestMethod.GET, produces = "text/html;charset=utf-8")
    public String searchTreeControlProcess(){
        List<ProductType> productTypes = productBreakageServiceInter.searchTreeControl();
        String treeData = JSON.toJSONString(productTypes).replaceAll("proTypeId","id").replaceAll("proLevelParId","parentId").replaceAll("proTypeName","title");
        return treeData;
    }
}
